﻿using System;
using AutoMapper;
using Framework.Exceptions;
using Interfaces.BusinessLogic;
using Interfaces.Models;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using ServiceInfrastructure.Models.SecurityModels.Models;

namespace BusinessLogic.Services.Mapping
{
    public class RoleContractMapper : IEntityMapper<IRole,Role>
    {
        private static void CreateMapping()
        {
            Mapper.Reset();
            Mapper.CreateMap<IRole, Role>();
            Mapper.CreateMap<IUser, User>()
                  .ForMember(x => x.Roles, opt => opt.Ignore());
        }

        public Role Map(IRole entity)
        {
            Role newRole = null;

            try
            {
                CreateMapping();

                newRole = Mapper.Map<IRole, Role>(entity);
            }
            catch (Exception exception)
            {
                if (ExceptionHandlingManager.HandleException(exception, PolicyNames.DALPolicy))
                    throw;
            }

            return newRole;
        }
    }
}
